package com.badibadi.fragment;

import com.badibadi.fragment.AfterLandingFriends01.XListView2Adapter;
import com.badibadi.uniclubber.R;
import com.view.my_view.XListView;
import com.view.my_view.XListView.IXListViewListener;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TabHost;

public class AfterLandingMyHomePageFriendsApplyForFragment extends BaseFragment {

	private TabHost tabhost;

	private XListView xListView2;

	private LinearLayout mLayout;

	private FragmentManager manager;
	private AfterLandingFriends00 alfragment00;
	private AfterLandingFriends005 alfragment005;
	private AfterLandingFriends01 alfragment01;
	// AfterLandingFriends02 alfragment02;
	// AfterLandingFriends03 alfragment03;
	/** 保存加载的第几个fragment，加载过的就不再加载了 */
	private int add_in = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		manager = getFragmentManager();
		alfragment00 = new AfterLandingFriends00();
		alfragment005=new AfterLandingFriends005();
		alfragment01 = new AfterLandingFriends01();
		// alfragment02=new AfterLandingFriends02();
		// alfragment03=new AfterLandingFriends03();

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(
				R.layout.fragment_afterlanding_frednds_applyfor, null);
		init(view);
		/** 初始化fragment */
		initFragment();

		initTabHost(view);

		// InitMyXListView2(view);
		return view;
	}

	private void initFragment() {
		FragmentTransaction transaction = manager.beginTransaction();
		transaction.replace(R.id.fragment_friends_layout, alfragment00);
		transaction.commit();
	}

	private void init(View view) {
		mLayout = (LinearLayout) view
				.findViewById(R.id.fragment_friends_layout);
	}

	@Override
	public void onPause() {
		super.onPause();
	}

	private void initTabHost(View view) {
		tabhost = (TabHost) view
				.findViewById(R.id.fragment_after_landing_friends_tabhost);

		tabhost.setup();
		tabhost.addTab(tabhost
				.newTabSpec("fragment_after_landing_friends_tabhost one")
				.setIndicator(
						getResources().getString(R.string.Friends_validation),
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.tab1));
		tabhost.addTab(tabhost
				.newTabSpec("fragment_after_landing_friends_tabhost two")
				.setIndicator(getResources().getString(R.string.ClubInvitation),
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.tab2));
		
		
		tabhost.addTab(tabhost
				.newTabSpec("fragment_after_landing_friends_tabhost three")
				.setIndicator("@" + getResources().getString(R.string.Me),
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.tab3));
		// tabhost.addTab(tabhost.newTabSpec("fragment_after_landing_friends_tabhost three").setIndicator(getResources().getString(R.string.TheClubApplyFor),getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.tab3));
		// tabhost.addTab(tabhost.newTabSpec("fragment_after_landing_friends_tabhost four").setIndicator(getResources().getString(R.string.TheClubContact),getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.tab4));

		tabhost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
			@Override
			public void onTabChanged(String arg0) {

				FragmentTransaction transaction = manager.beginTransaction();
				switch (tabhost.getCurrentTab()) {
				case 0:
					if (add_in != 0) {
						transaction.replace(R.id.fragment_friends_layout,
								alfragment00);
						transaction.commit();
						add_in = 0;
					}
					break;
				case 1:
					if (add_in != 1) {
						transaction.replace(R.id.fragment_friends_layout,
								alfragment005);
						transaction.commit();
						add_in = 1;
					}
					break;
				case 2:
					if (add_in != 2) {
						transaction.replace(R.id.fragment_friends_layout,
								alfragment01);
						transaction.commit();
						add_in = 2;
					}
					break;
				// case 2:
				// if(add_in!=2)
				// {
				// transaction.replace(R.id.fragment_friends_layout,
				// alfragment02);
				// transaction.commit();
				// add_in=2;
				// }
				// break;
				// case 3:
				// if(add_in!=3)
				// {
				// transaction.replace(R.id.fragment_friends_layout,
				// alfragment03);
				// transaction.commit();
				// add_in=3;
				// }
				// break;
				}
			}
		});

	}
}

/** 第一个fragment 好友验证 */
class AfterLandingFriends00 extends Fragment implements IXListViewListener {
	// TODO Auto-generated method stub
	private XListView1Adapter x1Adapter;
	private XListView xListView1;
	/** 每次加入的数量 */
	private int addNumber = 20;
	private Handler mHandler;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		x1Adapter = new XListView1Adapter();
		mHandler = new Handler();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.after_landing_friends00, null);
		InitMyXListView1(view);

		// mListView.setPullLoadEnable(true);

		// mListView.setAdapter(x1Adapter);
		return view;
	}

	public void InitMyXListView1(View view) {
		xListView1 = (XListView) view
				.findViewById(R.id.after_landing_friends00_xlistView);
		xListView1.setPullLoadEnable(true);
		xListView1.setAdapter(x1Adapter);
		xListView1.setXListViewListener(this);
	}

	@Override
	public void onRefresh() {
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				geneItems();

				x1Adapter.notifyDataSetInvalidated();
				onLoad();
			}
		}, 2000);
	}

	protected void onLoad() {
		xListView1.stopRefresh();
		xListView1.stopLoadMore();
		xListView1.setRefreshTime(getResources().getString(R.string.wx_ganggang));
	}

	@Override
	public void onLoadMore() {
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				geneItems();

				x1Adapter.notifyDataSetChanged();
				onLoad();
			}
		}, 2000);
	}

	protected void geneItems() {
		// x1Adapter.setAddNumber(x1Adapter.getAddNumber()+20);
		addNumber = addNumber + 20;
	}

	class XListView1Adapter extends BaseAdapter {

		@Override
		public int getCount() {

			return addNumber;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater inflater = LayoutInflater.from(getActivity());
			View view = inflater.inflate(
					R.layout.fragment_afterlanding_friends_import_layout, null);

			return view;
		}

	}
}

/**
 * 第二个fragment 俱乐部邀请
 * 
 * // TODO Auto-generated method stub
 **/
class AfterLandingFriends005 extends Fragment implements IXListViewListener {
	private XListView2Adapter x2Adapter;
	private XListView xListView2;

	private Handler mHandler;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		x2Adapter = new XListView2Adapter();
		mHandler = new Handler();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.after_landing_friends00, null);
		InitMyXListView2(view);

		// mListView.setPullLoadEnable(true);

		// mListView.setAdapter(x1Adapter);
		return view;
	}

	public void InitMyXListView2(View view) {
		xListView2 = (XListView) view
				.findViewById(R.id.after_landing_friends00_xlistView);
		xListView2.setPullLoadEnable(true);
		xListView2.setAdapter(x2Adapter);
		xListView2.setXListViewListener(this);
	}

	@Override
	public void onRefresh() {
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				geneItems();

				x2Adapter.notifyDataSetInvalidated();
				onLoad();
			}
		}, 2000);
	}

	protected void onLoad() {
		xListView2.stopRefresh();
		xListView2.stopLoadMore();
		xListView2.setRefreshTime(getResources().getString(R.string.wx_ganggang));
	}

	@Override
	public void onLoadMore() {
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				geneItems();

				x2Adapter.notifyDataSetChanged();
				onLoad();
			}
		}, 2000);
	}

	protected void geneItems() {
		x2Adapter.setAddNumber(x2Adapter.getAddNumber() + 20);
	}

	class XListView2Adapter extends BaseAdapter {

		private int addNumber = 20;

		@Override
		public int getCount() {
			return addNumber;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater inflater = LayoutInflater.from(getActivity());
			View view = inflater.inflate(
					R.layout.club_invitation, null);
//club_invite_img_add
			return view;
		}

		public int getAddNumber() {
			return addNumber;
		}

		public void setAddNumber(int addNumber) {
			this.addNumber = addNumber;
		}
	}
}


/**
 * 第三个fragment 提到我
 * 
 * // TODO Auto-generated method stub
 **/
class AfterLandingFriends01 extends Fragment implements IXListViewListener {
	private XListView2Adapter x2Adapter;
	private XListView xListView2;

	private Handler mHandler;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		x2Adapter = new XListView2Adapter();
		mHandler = new Handler();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.after_landing_friends00, null);
		InitMyXListView2(view);

		// mListView.setPullLoadEnable(true);

		// mListView.setAdapter(x1Adapter);
		return view;
	}

	public void InitMyXListView2(View view) {
		xListView2 = (XListView) view
				.findViewById(R.id.after_landing_friends00_xlistView);
		xListView2.setPullLoadEnable(true);
		xListView2.setAdapter(x2Adapter);
		xListView2.setXListViewListener(this);
	}

	@Override
	public void onRefresh() {
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				geneItems();

				x2Adapter.notifyDataSetInvalidated();
				onLoad();
			}
		}, 2000);
	}

	protected void onLoad() {
		xListView2.stopRefresh();
		xListView2.stopLoadMore();
		xListView2.setRefreshTime(getResources().getString(R.string.wx_ganggang));
	}

	@Override
	public void onLoadMore() {
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				geneItems();

				x2Adapter.notifyDataSetChanged();
				onLoad();
			}
		}, 2000);
	}

	protected void geneItems() {
		x2Adapter.setAddNumber(x2Adapter.getAddNumber() + 20);
	}

	class XListView2Adapter extends BaseAdapter {

		int addNumber = 20;

		@Override
		public int getCount() {
			return addNumber;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater inflater = LayoutInflater.from(getActivity());
			View view = inflater.inflate(
					R.layout.fragment_afterlanding_tidaome_import_layout, null);

			return view;
		}

		public int getAddNumber() {
			return addNumber;
		}

		public void setAddNumber(int addNumber) {
			this.addNumber = addNumber;
		}
	}
}

// /**
// * 第三个fragment 申请表
// *
// *
// **/
// class AfterLandingFriends02 extends Fragment implements IXListViewListener{
// XListView1Adapter x1Adapter;
// XListView xListView1;
//
// private Handler mHandler;
// @Override
// public void onCreate(Bundle savedInstanceState) {
// // TODO Auto-generated method stub
// super.onCreate(savedInstanceState);
// x1Adapter=new XListView1Adapter();
// mHandler=new Handler();
// }
// @Override
// public View onCreateView(LayoutInflater inflater, ViewGroup container,
// Bundle savedInstanceState) {
// // TODO Auto-generated method stub
//
// View view=inflater.inflate(R.layout.after_landing_friends00, null);
// InitMyXListView1(view);
//
// //mListView.setPullLoadEnable(true);
//
// // mListView.setAdapter(x1Adapter);
// return view;
// }
// public void InitMyXListView1(View view) {
// xListView1 =
// (XListView)view.findViewById(R.id.after_landing_friends00_xlistView);
// xListView1.setPullLoadEnable(true);
// xListView1.setAdapter(x1Adapter);
// xListView1.setXListViewListener(this);
// }
// @Override
// public void onRefresh() {
// // TODO Auto-generated method stub
// mHandler.postDelayed(new Runnable() {
// @Override
// public void run() {
// geneItems();
//
// x1Adapter.notifyDataSetInvalidated();
// onLoad();
// }
// }, 2000);
// }
// protected void onLoad() {
// // TODO Auto-generated method stub
// xListView1.stopRefresh();
// xListView1.stopLoadMore();
// xListView1.setRefreshTime("刚刚");
// }
// @Override
// public void onLoadMore() {
// // TODO Auto-generated method stub
// mHandler.postDelayed(new Runnable() {
// @Override
// public void run() {
// geneItems();
//
// x1Adapter.notifyDataSetChanged();
// onLoad();
// }
// }, 2000);
// }
// protected void geneItems() {
// // TODO Auto-generated method stub
// x1Adapter.setAddNumber(x1Adapter.getAddNumber()+20);
// }
// class XListView1Adapter extends BaseAdapter{
//
// int addNumber=20;
// @Override
// public int getCount() {
// // TODO Auto-generated method stub
// return addNumber;
// }
//
// @Override
// public Object getItem(int position) {
// // TODO Auto-generated method stub
// return null;
// }
//
// @Override
// public long getItemId(int position) {
// // TODO Auto-generated method stub
// return 0;
// }
//
// @Override
// public View getView(int position, View convertView, ViewGroup parent) {
// // TODO Auto-generated method stub
// LayoutInflater inflater=LayoutInflater.from(getActivity());
// View
// view=inflater.inflate(R.layout.fragment_club_applicationform_import_layout,
// null);
//
// return view;
// }
//
// public int getAddNumber() {
// return addNumber;
// }
//
// public void setAddNumber(int addNumber) {
// this.addNumber = addNumber;
// }
//
// }
// }
//
//
//
//
//
//
// /**
// * 第四个fragment 俱乐部联系
// * @author Administrator
// *
// */
// class AfterLandingFriends03 extends Fragment implements IXListViewListener{
// XListView1Adapter x1Adapter;
// XListView xListView1;
//
// private Handler mHandler;
// @Override
// public void onCreate(Bundle savedInstanceState) {
// // TODO Auto-generated method stub
// super.onCreate(savedInstanceState);
// x1Adapter=new XListView1Adapter();
// mHandler=new Handler();
// }
// @Override
// public View onCreateView(LayoutInflater inflater, ViewGroup container,
// Bundle savedInstanceState) {
// // TODO Auto-generated method stub
//
// View view=inflater.inflate(R.layout.after_landing_friends00, null);
// InitMyXListView1(view);
//
// //mListView.setPullLoadEnable(true);
//
// // mListView.setAdapter(x1Adapter);
// return view;
// }
// public void InitMyXListView1(View view) {
// xListView1 =
// (XListView)view.findViewById(R.id.after_landing_friends00_xlistView);
// xListView1.setPullLoadEnable(true);
// xListView1.setAdapter(x1Adapter);
// xListView1.setXListViewListener(this);
// }
// @Override
// public void onRefresh() {
// // TODO Auto-generated method stub
// mHandler.postDelayed(new Runnable() {
// @Override
// public void run() {
// geneItems();
//
// x1Adapter.notifyDataSetInvalidated();
// onLoad();
// }
// }, 2000);
// }
// protected void onLoad() {
// // TODO Auto-generated method stub
// xListView1.stopRefresh();
// xListView1.stopLoadMore();
// xListView1.setRefreshTime("刚刚");
// }
// @Override
// public void onLoadMore() {
// // TODO Auto-generated method stub
// mHandler.postDelayed(new Runnable() {
// @Override
// public void run() {
// geneItems();
//
// x1Adapter.notifyDataSetChanged();
// onLoad();
// }
// }, 2000);
// }
// protected void geneItems() {
// // TODO Auto-generated method stub
// x1Adapter.setAddNumber(x1Adapter.getAddNumber()+3);
// }
//
//
//
// class XListView1Adapter extends BaseAdapter{
//
// int addNumber=3;
// @Override
// public int getCount() {
// // TODO Auto-generated method stub
// return addNumber;
// }
//
// @Override
// public Object getItem(int position) {
// // TODO Auto-generated method stub
// return null;
// }
//
// @Override
// public long getItemId(int position) {
// // TODO Auto-generated method stub
// return 0;
// }
//
// @Override
// public View getView(int position, View convertView, ViewGroup parent) {
// // TODO Auto-generated method stub
// LayoutInflater inflater=LayoutInflater.from(getActivity());
// View view=inflater.inflate(R.layout.fragment_club_lianxi, null);
//
// return view;
// }
//
// public int getAddNumber() {
// return addNumber;
// }
//
// public void setAddNumber(int addNumber) {
// this.addNumber = addNumber;
// }
//
// }
// }